package com.yunfei.api.v2.qiuzhi.service.impl;

import com.yunfei.api.v2.qiuzhi.contants.QiuZhiContants;
import com.yunfei.api.v2.qiuzhi.service.DianZanService;
import com.yunfei.api.v2.user.valid.DianZanValid;
import com.yunfei.api.v2.user.valid.TokenForm;
import com.yunfei.common.json.result.ResultModel;
import com.yunfei.core.utils.TokenUtil;
import com.yunfei.core.utils.enmu.QuesResultStatus;
import com.yunfei.core.utils.enmu.ResultStatus;
import com.yunfei.core.utils.string.StringUtil;
import com.yunfei.mapper.aiiidb.UserLikeLogMapper;
import com.yunfei.bo.UserLikeLogBo;
import com.yunfei.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: zntg_v1
 * @description:点赞
 * @author: zhengyunfei
 * @create: 2018-10-30 15:48
 **/
@Service("dianzanService")
public class DianZanServiceImpl implements DianZanService {
    @Autowired
    private UserLikeLogMapper userLikeLogMapper;
    @Autowired
    private RedisService redisService;
    /**
     * @Description: 求知广场:点赞
     * @Author: zhengyunfei
     * @Date: 2018/11/1
     * @time:10:36
     */
    public ResultModel dianzan(DianZanValid requestParam, TokenForm tokenForm, ResultModel resultModel) {
        String browMdl=requestParam.getBrowMdl();//浏览模块
        String operFlg=requestParam.getOperFlg();//操作类型
        String likeDtlId=requestParam.getLikeDtlId();//浏览内容ID
        String token=tokenForm.getToken();
        if(StringUtil.isNullOrEmpty(token)){
            resultModel.setCode(ResultStatus.TOKEN_NOTNULL.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_NOTNULL.getMessage());
            return resultModel;
        }
        if(token.length()<64){
            resultModel.setCode(ResultStatus.TOKEN_WUXIAO.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_WUXIAO.getMessage());
            return resultModel;
        }
        if(StringUtil.isNullOrEmpty(browMdl)){
            resultModel.setCode(QuesResultStatus.BROW_MDL_NOTNULL.getCode());
            resultModel.setMessage(QuesResultStatus.BROW_MDL_NOTNULL.getMessage());
            return resultModel;
        }else if(!browMdl.equals(QiuZhiContants.BROW_MDL_QUES)&&!browMdl.equals(QiuZhiContants.BROW_MDL_BL)&&!browMdl.equals(QiuZhiContants.BROW_MDL_PROD)){
            resultModel.setCode(QuesResultStatus.BROW_MDL_NOTEXSITS.getCode());
            resultModel.setMessage(QuesResultStatus.BROW_MDL_NOTEXSITS.getMessage());
            return resultModel;
        }
        //校验点赞类型
        if(StringUtil.isNullOrEmpty(operFlg)){
            resultModel.setCode(QuesResultStatus.OPER_FLG_NOTNULL.getCode());
            resultModel.setMessage(QuesResultStatus.OPER_FLG_NOTNULL.getMessage());
            return resultModel;
        }else if(!operFlg.equals(QiuZhiContants.OPER_FLG_Y)&&!operFlg.equals(QiuZhiContants.OPER_FLG_N)){
            resultModel.setCode(QuesResultStatus.OPER_FLG_NOTEXSITS.getCode());
            resultModel.setMessage(QuesResultStatus.OPER_FLG_NOTEXSITS.getMessage());
            return resultModel;
        }
        if(redisService.exists(token)){
            UserLikeLogBo userLikeLogBo=new UserLikeLogBo();
            String userId= TokenUtil.getInstance().getUserId(token);
            userLikeLogBo.setUserId(userId);
            userLikeLogBo.setBrowMdl(browMdl);
            userLikeLogBo.setOperFlg(operFlg);
            userLikeLogBo.setLikeDtlId(likeDtlId);
            userLikeLogMapper.add(userLikeLogBo);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            return resultModel;

        }else{
            resultModel.setCode(ResultStatus.TOKEN_ERROR.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_ERROR.getMessage());
            return resultModel;
        }
    }


}
